package com.liu.io.file.pdf;

import com.lowagie.text.*;
import com.lowagie.text.pdf.draw.DottedLineSeparator;
import com.lowagie.text.pdf.draw.LineSeparator;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author liubo
 */
public abstract class GeneratePdf {

    /**
     * 生成PDF文件
     *
     * @param document
     * @param pdfData
     * @throws Exception
     */
    public abstract void generatePdf(Document document, List<List<String>> pdfData) throws Exception;

    /**
     * 获取需要导出数据
     * @param pdfData
     * @param pdfTitle
     * @return
     */
    public abstract List<List<String>> getPdfData(Map<String, Object> pdfData, String pdfTitle);

    /**
     * 备件PDF导出
     * @param pdfData
     * @param fileName
     * @return
     * @throws Exception
     */
    public abstract String createPDF(List<List<String>> pdfData, String fileName) throws Exception;

    /**
     * 段落
     *
     * @return
     */
    public Paragraph createParagraph(String title, Font titlefont) {
        Paragraph paragraph = new Paragraph(title, titlefont);
        return paragraph;
    }

    /**
     * 直线
     *
     * @return
     */
    public Paragraph createLineSeparator() {
        Paragraph paragraph = new Paragraph();
        paragraph.add(new Chunk(new LineSeparator()));
        return paragraph;
    }

    /**
     * 点线
     *
     * @return
     */
    public Paragraph createDottedLineSeparator() {
        Paragraph paragraph = new Paragraph();
        paragraph.add(new Chunk(new DottedLineSeparator()));
        return paragraph;
    }

    /**
     * 超链接
     *
     * @param anchorName
     * @param anchorURL
     * @return
     */
    public Anchor createAnchor(String anchorName, String anchorURL) {
        Anchor anchor = new Anchor(anchorName);
        anchor.setReference(anchorURL);
        return anchor;
    }

    /**
     * 定位
     *
     * @param top
     * @return
     */
    public Anchor createReference(String top) {
        Anchor gotoP = new Anchor("goto");
        gotoP.setReference(top);
        return gotoP;
    }

    /**
     * 图片
     *
     * @param imageURL
     * @param scalePercent 依照比例缩放
     * @return
     * @throws IOException
     * @throws BadElementException
     */
    public Image createImage(String imageURL, int scalePercent) throws IOException, BadElementException {
        Image image = Image.getInstance(imageURL);
        image.setAlignment(Image.ALIGN_CENTER);
        image.scalePercent(scalePercent);
        return image;
    }

}
